<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Admin\Course;
use \App\Admin\Protype;
use \App\Admin\Profession;
use \App\Admin\Performer;
use Input;
class CourseController extends Controller
{
    public function index(){
    	//获取数据
        $protype_id = Input::get('protype_id');
        $protype_name = Protype::select('protype_name') -> where('id',$protype_id) -> get();
    	$data = Course::select('course.*','profession.pro_name','protype.protype_name') -> where("protype.id",$protype_id) -> orderBy('id','desc') -> leftJoin('profession','profession_id','=','profession.id') -> leftJoin('protype','protype_id','=','protype.id') -> get();
    	return view('admin.course.index',['data' => $data ,'protype_id' => $protype_id ,'protype_name' => $protype_name]);
    }

    public function add(){
        if(Input::method() == 'POST'){
            //提交处理
            //自动验证
            //接收数据
            $data = Input::all();
            //写入数据表
            $result = Course::insert([
                'course_name'   =>	$data['course_name'],
                'profession_id'	=>	$data['profession_id'],
                'cover_img'		=>	$data['cover_img'],
                'banner_img'    =>	$data['banner_img'],
                'carousel_img'  =>	$data['carousel_img'],
                'big_banner_img'=>	$data['big_banner_img'],
                'description'	=>	$data['description'],
                'update_number' => $data['update_number'],
                'click_num'	    =>	$data['click_num'],
                'created_at'	=>	date('Y-m-d H:i:s'),
                'sort'			=>	$data['sort'],
                'status'        => 2,
                'type'          => $data['type'],
                'free'          => $data['free'],
                'price'         => $data['price'],
                'course_detail' => $data['course_detail'],
                'recommend_type'=> $data['recommend_type'],
                'is_show_mobile'=> $data['is_show_mobile'],
                'performer_ids' => empty($data['performer_ids']) == true ? '' : implode(',',$data['performer_ids']),

            ]);
            session(['cover_img_url' => null]);
            session(['banner_img_url' => null]);
            session(['carousel_img_url' => null]);
            //返回
            return $result ? '1' : '0';
        }else{
            $type  = Protype::get();
            $stars = Performer::select('id','avatar_img','performer_name') -> get();
            return view('admin.course.add',['type' => $type ,'stars' => $stars]);
        }
    }

    public function edit(){
        if(Input::method() == 'POST'){
            $data   = Input::all();
            $result = Course::where('id',$data['id']) -> update([
                'course_name'   => $data['course_name'] ? $data['course_name'] : '',
                'update_number' => $data['update_number'] ? $data['update_number'] : '',
                'profession_id'	=> $data['profession_id'] ? $data['profession_id'] : '',
                'cover_img'		=> $data['cover_img'] ? $data['cover_img'] : '',
                'banner_img'    => $data['banner_img'] ? $data['banner_img'] : '',
                'carousel_img'  => $data['carousel_img'] ? $data['carousel_img'] : '',
                'big_banner_img'=> $data['big_banner_img'] ? $data['big_banner_img'] : '',
                'description'	=> $data['description'] ? $data['description'] : '',
                'click_num'	    => $data['click_num'] ? $data['click_num'] : 100,
                'sort'			=> $data['sort'] ? $data['sort'] : 0,
                'status'        => $data['status'],
                'type'          => $data['type'],
                'free'          => $data['free'],
                'price'         => $data['price'] ? $data['price'] : '0.00',
                'course_detail' => $data['course_detail'] ? $data['course_detail'] : '',
                'recommend_type'=> $data['recommend_type'],
                'is_show_PC'    => $data['is_show_PC'],
                'is_show_mobile'=> $data['is_show_mobile'],
                'is_home_page_show'=> $data['is_home_page_show'],
                'performer_ids' => empty($data['performer_ids']) == true ? '' : implode(',',$data['performer_ids']),
            ]);
            session(['cover_img_url' => null]);
            session(['banner_img_url' => null]);
            session(['carousel_img_url' => null]);
            return $result ? '1' : '0';
        }else{
            $id    = Input::get('id');
            $type  = Protype::get();
            $stars = Performer::select('id','avatar_img','performer_name') -> get();
            $data  = Course::select('course.*','profession.pro_name','profession.protype_id','protype.protype_name') -> where('course.id',$id) -> leftJoin('profession','profession_id','=','profession.id') -> leftJoin('protype','protype_id','=','protype.id') -> first();
            $data['performer_ids'] = explode(',',$data['performer_ids']);
            $pro  = Profession::select('id','pro_name') -> where('protype_id',$data['protype_id']) -> get();
            return view('admin.course.edit',['data' => $data ,'type' => $type , 'stars' => $stars ,'pro' => $pro]);
        }
    }

    public function edit_status(){
        $id = Input::get('id');
        $status = Input::get('status');
        $result = Course::where('id',$id) -> update([
           'status' => $status
        ]);
        return $result ? '1' : '0';
    }

    public function del(){
        $id = Input::get('id');
        $result = Course::where('id',$id) -> delete();
        return $result ? '1' : '0';
    }

    public function search(){
        $keyword = Input::get('key');
        $start_time = Input::get('datemin') ? Input::get('datemin') . ' 00:00:00' : '1999-01-01 00:00:00';
        $end_time = Input::get('datemax')? Input::get('datemax') . ' 23:59:59' : date('Y-m-d H:i:s');

        $protype_id = Input::get('protype_id');
        $protype_name = Protype::select('protype_name') -> where('id',$protype_id) -> get();

        $data = Profession::where('pro_name','LIKE',"%$keyword%") -> whereBetween('created_at',[$start_time,$end_time]) -> get();
        //展示视图，携带数据
        return view('admin.course.index',['data' => $data, 'key' => $keyword ,'datemin' => Input::get('datemin') , 'datemax' => Input::get('datemax'),'protype_id' => $protype_id ,'protype_name' => $protype_name]);
    }

    public function get_profession(){
        $id = Input::get('id');
        if(empty($id) == true){
            $data['status'] = 0;
            return response() -> json($data);
        }else{
            $data['info'] = Profession::select('id','pro_name') -> where('protype_id',$id) -> get();
            $data['status'] = 1;
            return response() -> json($data);
        }
    }

    public function get_lessions(){
        $id = Input::get('id');
        if(empty($id) == true){
            $data['status'] = 0;
        }else{
            $info = Course::select('course.id','course.course_name') -> where('course.id',$id) -> with('rel_lessions') -> first() -> toArray();
            if(empty($info['rel_lessions']) == true){
                $data['status'] = 0;
            }else{
                $data['info'] = $info;
                $data['status'] = 1;
            }
        }
        return response() -> json($data);
    }

}
